package com.sevennotes.classtable.utils

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.PagingState
import androidx.paging.cachedIn
import com.sevennotes.classtable.model.ClassRecord
import com.sevennotes.classtable.model.ClassRecordMonthStatis
import com.sevennotes.classtable.model.ClassRecordWithName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import java.util.Date
import java.util.Random

object FakeDataUtil {

  private val testPager = Pager(
    PagingConfig(pageSize = 5),
    pagingSourceFactory = { TestPagingSource() }
  )

  val testPagerFakeData = testPager.flow.cachedIn(CoroutineScope(Dispatchers.Main))

  class TestPagingSource : PagingSource<Int, ClassRecordWithName>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ClassRecordWithName> {
      val data = List(100) {
        ClassRecordWithName(
          ClassRecord(
            gpChange = it + 1,
            startTime = Date(),
            duration = (it + 1) * 10,
            note = "Note $it"
          ),
          studentName = "Student $it"
        )
      }
      return LoadResult.Page(
        data = data,
        prevKey = null,
        nextKey = null
      )
    }

    override val keyReuseSupported: Boolean = true
    override fun getRefreshKey(state: PagingState<Int, ClassRecordWithName>): Int? {
      // 获取当前列表中的第一个元素
      val firstRecord = state.pages.firstOrNull()?.data?.firstOrNull()
      // 如果第一个元素不为空，则以它的 gpChange 字段作为刷新键
      return firstRecord?.classRecord?.gpChange
    }
  }

  fun getClassRecordStatisWithYears(): List<Pair<String, List<ClassRecordMonthStatis>>> {
    val res = mutableListOf<Pair<String, List<ClassRecordMonthStatis>>>()
    (2018..2023).forEach {
      res.add(
        Pair(it.toString(), getClassRecordStatisData())
      )
    }
    return res
  }

  fun getClassRecordStatisData(): List<ClassRecordMonthStatis> {
    return listOf(
      ClassRecordMonthStatis(
        month = "01",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "02",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "03",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "04",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "05",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "06",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "07",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "08",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "09",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "10",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "11",
        total = Random().nextInt(200)
      ),
      ClassRecordMonthStatis(
        month = "12",
        total = Random().nextInt(200)
      ),
    )
  }

}